package com.aji.leather.controller;

import com.aji.allcommon.result.Result;
import com.aji.allcommon.service.FileStorageService;
import com.aji.allcommon.utills.MultipartFileUtill;
import com.aji.leather.domain.Leather;
import com.aji.leather.domain.LeatherConditionDto;
import com.aji.leather.service.LeatherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/leather")
@Api(tags = "皮革模块")
@CrossOrigin
@RequiredArgsConstructor
public class LeatherController {

    private final LeatherService leatherService;
    private final FileStorageService fileStorageService;

    @ApiOperation("图片转url地址")
    @PostMapping("/imgToUrl")
    public Result imgToUrl(MultipartFile imgFile) throws IOException {
        String originalFilename = imgFile.getOriginalFilename();
        String simpFileName = originalFilename.substring(0, originalFilename.length() - 4) + ".jpg";
        File fileimg = File.createTempFile("temp", null);
        imgFile.transferTo(fileimg);
        FileInputStream simpJpgFile = MultipartFileUtill.multipartTifToJpgFileInputStream(fileimg);
        String accessSimpFileName = fileStorageService.uploadFile("img/jpg", simpFileName, simpJpgFile, "img/jpg");
        return Result.success(accessSimpFileName);
    }
    @ApiOperation("新增皮革信息")
    @PostMapping()
    public Result save(@RequestBody Leather entity){
        entity.setCreateTime(new Date());
        leatherService.save(entity);
        return Result.success();
    }
    @ApiOperation("删除皮革信息")
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        leatherService.removeByIds(ids);
        return Result.success("删除成功！");
    }
    @ApiOperation("修改样品信息")
    @PutMapping()
    public Result updateById(@RequestBody Leather entity){
        System.out.println(entity);
        leatherService.updateById(entity);
        return Result.success();
    }
    @ApiOperation("查询皮革信息 根据id")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        return Result.success(leatherService.getById(id));
    }

    @ApiOperation("查询全部皮革信息")
    @GetMapping("/list")
    public Result list(){
        return Result.success(leatherService.list(null));
    }

    @ApiOperation("条件分页查询")
    @GetMapping("/{page}/{pageSize}")
    public Result listByConditionAndPage(@PathVariable int page, @PathVariable int pageSize, LeatherConditionDto compound){
        //合法检验
        if(page<1||pageSize<1){return Result.error("分页参数异常");}
        Page<Leather> compoundPage= new Page<>(page,pageSize);
        LambdaQueryWrapper<Leather> compoundLambdaQueryWrapper  =new LambdaQueryWrapper<Leather>().select()
                .like(compound.getCollector()!=null,Leather::getCollector,compound.getCollector())
                .like(compound.getLeatherClass()!=null,Leather::getLeatherClass,compound.getLeatherClass())
                .like(compound.getLeatherName()!=null,Leather::getLeatherName,compound.getLeatherName())
                .like(compound.getLeatherNumber()!=null,Leather::getLeatherNumber,compound.getLeatherNumber())
                .like(compound.getPlace()!=null,Leather::getPlace,compound.getPlace());

        IPage<Leather> compoundIPage = leatherService.page(compoundPage, compoundLambdaQueryWrapper);
        return Result.success(compoundIPage);

    }

    @ApiOperation("获取数据总量")
    @GetMapping("/count")
    public Result count(){
        int count = Math.toIntExact(leatherService.count());
        return Result.success(count);
    }
}
